package com.zm.zb100.service;

import com.zm.zb100.persistence.entity.db.Demo;
import com.zm.zb100.repository.DemoRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * DemoService
 *
 * <p>
 * </p>
 *
 * @author chuanbai
 * @date 2024/9/30
 */
@Service
public class DemoService {


    @Resource
    private DemoRepository demoRepository;


    @Resource
    private Demo0 demo0;

    /**
     * 外层事务
     */
    @Transactional
    public String outerInsert(){
        try {
            Demo demo = new Demo();
            demo.setLogisticsNo("outerInsert");
            Demo save = demoRepository.save(demo);

            // 执行内层事物
            String string = demo0.innerInsert();
            System.out.println(string);

            int i = 10 / 0;
            System.out.println("outerInsert   end-------->");
            return "outerInsert ok";
        }catch (Exception e){
            System.out.println("outerInsert error.");
            throw new RuntimeException("outerInsert error", e); // 显式抛出运行时异常
            // 外层事务捕获到异常不抛出，触发外层事物生效
//            return "error";
        }
    }


}
